package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.PerformanceSetService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetCheckDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetListByPermissionDTO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetCheckVO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetListByPermissionVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: 业绩目标指标
 * @Author: hongxiao
 * @date: 2019-02-15 09:51
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/performanceSet")
public class MobilePerformanceSetController {
    @Resource
    private PerformanceSetService performanceSetService;

    /**
     * 业绩目标指标是否已配置
     * @param performanceSetCheckDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-15 09:54
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String check(@RequestBody @Valid PerformanceSetCheckDTO performanceSetCheckDTO, BindingResult br) {
        XbbResponse<PerformanceSetCheckVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetCheckVO performanceSetCheckVO = performanceSetService.check(performanceSetCheckDTO);
                response = new XbbResponse<>(performanceSetCheckVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取登录员工能看到的指标列表
     * 参数：companyStructType必填
     *
     * @param performanceSetListByPermissionDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-15 13:57
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/listByPermission", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String listByPermission(@RequestBody @Valid PerformanceSetListByPermissionDTO performanceSetListByPermissionDTO, BindingResult br) {
        XbbResponse<PerformanceSetListByPermissionVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetListByPermissionVO performanceSetListByPermissionVO = performanceSetService.listByPermission(performanceSetListByPermissionDTO);
                response = new XbbResponse<>(performanceSetListByPermissionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
